#coding:utf-8
'''
* coder  : dzlua
* email  : 505544956@qq.com
* module : api-frame
* path   : app/apis
* file   : test.py
* time   : 2019-06-25 17:10:00
'''
#--------------------#
from flask import g
from flask_restful import Resource
from flask_restful import reqparse
from flask_restful import inputs
#----------#
from app.mgr import auth
from app.mgr import api
from app.mgr import db
#----------#
from app.models.user import User
#----------#
from app.utils.response import *
#--------------------#

#--------------------#
@api.resource('/users')
class Users(Resource):
    decorators = [auth.required_access_token]
    #
    def post(self):
        data = db.session.query(User.username).all()
        users = []
        for user in data:
            users.append(user[0])
        return makeResponse( R200_OK, users)
#--------------------#

#--------------------#
@api.resource('/info')
class Info(Resource):
    decorators = [auth.required_access_token]

    parser = reqparse.RequestParser()
    parser.add_argument('username', required=True,
        type=inputs.regex(r'^[A-Za-z_][A-Za-z_0-9]{2,9}$'),
        help='The user name format is correct?')
    #
    def post(self):
        args = self.parser.parse_args()
        username = args.get('username')

        user = User.get(username=username)
        if not user:
            return makeResponse(R400_BADREQUEST,
                msg='username %s not exist.' % username)

        return makeResponse( R200_OK, {
            'username': g.user.username,
            'email': g.user.email,
            'phone': g.user.phone,
            'email_verifyed': g.user.email_verifyed,
            'phone_verifyed': g.user.phone_verifyed,
        })
#--------------------#